﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using Jon.Adventureworks.Core.Data;
using Jon.Adventureworks.Core.ExtensionMethods;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Jon.Adventureworks.Core.Objects;
using System.Collections.Generic;

namespace Jon.Adventureworks.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {
        public UnitTest1()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void InsertContactRows()
        {
            AdventureWorksContextDataContext advData = new AdventureWorksContextDataContext();
            Random rand = new Random();
            Contact con;
            int rowcount = 100000 ;

            for (int i = 0; i < rowcount; i++)
            {
                con = new Contact();
                con.NameStyle = false;
                con.Title = "Mr.";
                con.FirstName = "Joe_" + rand.Next(100000, 999999).ToString();
                con.LastName = "Smith_" + rand.Next(100000, 999999).ToString();
                con.MiddleName = "P";
                con.Suffix = null;
                con.EmailAddress = "j." + con.LastName + "@something.com";
                con.EmailPromotion = rand.Next(0, 3);
                con.Phone = "555-867-5309";
                con.PasswordHash = "GylyRwiKnyNPKbC1r4FSqA5YN9shIgsNik5ADyqStZc=";
                con.PasswordSalt = "TVGHbhY=";
                con.AdditionalContactInfo = null;
                con.rowguid = Guid.NewGuid();
                con.ModifiedDate = DateTime.Now;

                advData.Contacts.InsertOnSubmit(con);
                advData.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
            }
        }

        [TestMethod]
        public void Retrive_And_Convert_To_XML()
        {
            AdventureWorksContextDataContext advData = new AdventureWorksContextDataContext();
            List<Contact> contacts = advData.Contacts.Take(10000).ToList<Contact>();
            DataContractSerializer serializer = new DataContractSerializer(typeof(List<Contact>));
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb);

            serializer.WriteObject(writer, contacts);
            writer.Close();

            //Console.WriteLine(sb.ToString());
            Console.WriteLine(contacts.ToXmlString<List<Contact>>());

        }

        //[TestMethod]
        //public void TestAnimalClassFromAssessment()
        //{
        //    Animal objFish = new Fish();
        //    Console.WriteLine(objFish.Breathe());
        //    Console.WriteLine(objFish.Eat());
        //    Console.WriteLine(objFish.Sleep());
        //}

        //[TestMethod]
        //public void TestAnimalClassFromAssessment()
        //{
        //    Animal objFish = new Fish();
        //    Console.WriteLine(objFish.Breathe());
        //    Console.WriteLine(objFish.Eat());
        //    Console.WriteLine(objFish.Sleep());
        //}
    }
}
